{"componentChunkName":"component---src-templates-post-jsx","path":"/[BOJ]2668/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","excerpt":"문제 세로 두 줄, 가로로 N개의 칸으로 이루어진 표가 있다. 첫째 줄의 각 칸에는 정수 1, 2, …, N이 차례대로 들어 있고 둘째 줄의 각 칸에는 1이상 N이하인 정수가 들어 있다. 첫째 줄에서 숫자를 적절히 뽑으면, 그 뽑힌 정수들이 이루는 집합과, 뽑힌 정수들의 바로 밑의 둘째 줄에 들어있는 정수들이 이루는 집합이 일치한다. 이러한 조건을 만족시…","html":"<h2>문제</h2>\n<p>세로 두 줄, 가로로 N개의 칸으로 이루어진 표가 있다. 첫째 줄의 각 칸에는 정수 1, 2, …, N이 차례대로 들어 있고 둘째 줄의 각 칸에는 1이상 N이하인 정수가 들어 있다. 첫째 줄에서 숫자를 적절히 뽑으면, 그 뽑힌 정수들이 이루는 집합과, 뽑힌 정수들의 바로 밑의 둘째 줄에 들어있는 정수들이 이루는 집합이 일치한다. 이러한 조건을 만족시키도록 정수들을 뽑되, 최대로 많이 뽑는 방법을 찾는 프로그램을 작성하시오. 예를 들어, N=7인 경우 아래와 같이 표가 주어졌다고 하자.</p>\n<img src='https://www.acmicpc.net/upload/images/u5JZnfExdtFXjmR.png' width='30%'>\n<p>이 경우에는 첫째 줄에서 1, 3, 5를 뽑는 것이 답이다. 첫째 줄의 1, 3, 5밑에는 각각 3, 1, 5가 있으며 두 집합은 일치한다. 이때 집합의 크기는 3이다. 만약 첫째 줄에서 1과 3을 뽑으면, 이들 바로 밑에는 정수 3과 1이 있으므로 두 집합이 일치한다. 그러나, 이 경우에 뽑힌 정수의 개수는 최대가 아니므로 답이 될 수 없다.</p>\n<h3>입력</h3>\n<ul>\n<li>첫째 줄에는 N(1≤N≤100)을 나타내는 정수 하나가 주어진다. 그 다음 줄부터는 표의 둘째 줄에 들어가는 정수들이 순서대로 한 줄에 하나씩 입력된다.</li>\n</ul>\n<h3>출력</h3>\n<ul>\n<li>첫째 줄에 뽑힌 정수들의 개수를 출력하고, 그 다음 줄부터는 뽑힌 정수들을 작은 수부터 큰 수의 순서로 한 줄에 하나씩 출력한다.</li>\n</ul>\n<h3>📍 <strong>Logic</strong></h3>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Graph {\n    int[] graph;\n    int[] visited;\n    PriorityQueue&lt;Integer&gt; path = new PriorityQueue&lt;&gt;();\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>입력되는 숫자들을 저장하는 <code class=\"language-text\">graph</code> 배열, 각 정점의 방문여부를 저장하는 <code class=\"language-text\">visited</code> 배열, 사이클을 이루는 정점들을 저장하는 <code class=\"language-text\">path</code> 우선순위 큐</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">public void solution() {\n    for (int i=1; i&lt;graph.length; i++) {\n        if (dfs(i, i) == 1) {\n            path.add(i);\n        }\n        visited = new int[graph.length];\n    }\n    printList();\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>각 정점을 시작으로 하여, <code class=\"language-text\">dfs</code> 의 리턴값으로 사이클의 존재여부 확인</li>\n<li>존재하는 경우, <code class=\"language-text\">path</code> 우선순위 큐에 저장</li>\n<li>방문 여부를 저장하는 <code class=\"language-text\">visited</code> 초기화</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">public int dfs (int start, int end) {\n    if (visited[end] == 0) {\n        visited[end] = 1;\n        return dfs(start, graph[end]);\n    }\n    else {\n        return (start == end) ? 1 : 0;\n    }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>시작점, 시작점에서 이어지는 정점을 인자로 받아, 방문여부를 확인하고 방문 체크</li>\n<li>\n<p>이어지는 정점이 방문한 경우, 시작점과 동일한지 확인</p>\n<ul>\n<li>동일한 경우, <strong>사이클이 존재</strong> → 1 반환</li>\n</ul>\n</li>\n</ul>\n<h3>📄 <strong>CODE</strong></h3>\n<details>\n  <summary>코드 보기/접기💫</summary>\n    <div markdown=\"1\" data-language=\"java\">\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">    import java.util.*;\n\n    class Graph {\n        int[] graph;\n        int[] visited;\n        PriorityQueue&lt;Integer&gt; path = new PriorityQueue&lt;&gt;();\n\n        public Graph(int size) {\n            graph = new int[size+1];\n            visited = new int[size+1];\n        }\n\n        public void AddElement(int idx, int item) {\n            graph[idx] = item;\n        }\n\n        public void solution() {\n            for (int i=1; i&lt;graph.length; i++) {\n                if (dfs(i, i) == 1) {\n                    path.add(i);\n                }\n                visited = new int[graph.length];\n            }\n            printList();\n        }\n\n        public int dfs (int start, int end) {\n            if (visited[end] == 0) {\n                visited[end] = 1;\n                return dfs(start, graph[end]);\n            }\n            else {\n                return (start == end) ? 1 : 0;\n            }\n        }\n\n        public void printList() {\n            System.out.println(path.size());\n            for (int t : path)\n                System.out.println(t);\n        }\n    }\n\n    public class Main {\n        public static void main(String[] args) {\n            Scanner sc = new Scanner(System.in);\n            int T = sc.nextInt();\n\n            Graph g = new Graph(T);\n\n            for (int i=1; i&lt;=T; i++) {\n                g.AddElement(i, sc.nextInt());\n            }\n\n            g.solution();\n\n            sc.close();\n        }\n    }</code>\n        </deckgo-highlight-code>\n  </div>\n</details>\n<h3>✏️ <strong>Review</strong></h3>\n<ul>\n<li>전에 <strong>C++</strong>로 푼 문제였는데, JAVA로 변환하면서 다시 한 번 아이디어를 생각해보았다.</li>\n<li>그래프 문제이기 때문에, 유향이든 무향이든 어쨌든 그래프로의 표현이 가능하다는 의미라고 생각해 정점 간의 인접 정보를 표현하고 이를 그려 아이디어를 생각해보았다.</li>\n<li>\n<p>사이클이 존재하는 경우, 사이클을 형성하는 PATH가 정답이 된다고 생각했다.</p>\n<ul>\n<li>방문한 정점에 다시 방문하는 경우로 구현하려고 했으나, 방문 여부 변경과 확인 시점에서 막혔다.</li>\n</ul>\n</li>\n</ul>\n<h3>📕 출처</h3>\n<p><a href=\"https://www.acmicpc.net/problem/2668\">https://www.acmicpc.net/problem/2668</a></p>","frontmatter":{"title":"👩‍💻 2668. 숫자 고르기","date":"January 13, 2022","update":"January 13, 2022","tags":["그래프이론","그래프탐색","DFS"],"series":"👩‍💻 BOJ"},"fields":{"slug":"/[BOJ]2668/","readingTime":{"minutes":5.13}}},"seriesList":{"edges":[{"node":{"id":"27733333-3aad-5771-a1a1-27b4de4b4a01","fields":{"slug":"/[BOJ]2225/"},"frontmatter":{"title":"👩‍💻 2225. 합분해"}}},{"node":{"id":"548ba188-2afe-5575-b79f-107c6c65b07d","fields":{"slug":"/[BOJ]1012/"},"frontmatter":{"title":"👩‍💻 1012. 유기농 배추"}}},{"node":{"id":"0c712680-130f-5fa5-8c3b-3632dd5d1af0","fields":{"slug":"/[BOJ]1260/"},"frontmatter":{"title":"👩‍💻 1260. DFS와 BFS"}}},{"node":{"id":"0e07a592-06ce-5e01-b5e1-95398330372e","fields":{"slug":"/[BOJ]1707/"},"frontmatter":{"title":"👩‍💻 1707. 이분그래프"}}},{"node":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","fields":{"slug":"/[BOJ]2668/"},"frontmatter":{"title":"👩‍💻 2668. 숫자 고르기"}}},{"node":{"id":"01bbb6dc-812c-5223-87c5-53bf04c0d0a6","fields":{"slug":"/[BOJ]9466/"},"frontmatter":{"title":"👩‍💻 9466. 텀 프로젝트"}}},{"node":{"id":"abc306b3-c3a6-552a-8979-e82d506b3921","fields":{"slug":"/[BOJ]2146/"},"frontmatter":{"title":"👩‍💻 2146. 다리 만들기"}}},{"node":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","fields":{"slug":"/[BOJ]5014/"},"frontmatter":{"title":"👩‍💻 5014. 스타트 링크"}}},{"node":{"id":"be297703-e778-5727-8019-6954bc95da3a","fields":{"slug":"/[BOJ]11559/"},"frontmatter":{"title":"👩‍💻 11559. Puyo Puyo"}}},{"node":{"id":"ddf11545-d468-5269-94b1-5daa2720e886","fields":{"slug":"/[BOJ]17471/"},"frontmatter":{"title":"👩‍💻 17471. 게리맨더링"}}},{"node":{"id":"8d77114d-fc6e-5de2-a3ca-c3b250425a56","fields":{"slug":"/[BOJ]2437/"},"frontmatter":{"title":"👩‍💻 2437. 저울"}}},{"node":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","fields":{"slug":"/[BOJ]14891/"},"frontmatter":{"title":"👩‍💻 14891. 톱니바퀴"}}},{"node":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","fields":{"slug":"/[BOJ]14503/"},"frontmatter":{"title":"👩‍💻 14503. 로봇 청소기"}}},{"node":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","fields":{"slug":"/[BOJ]3190/"},"frontmatter":{"title":"👩‍💻 3190. 뱀"}}},{"node":{"id":"099b2576-7d05-5872-a9f0-f1ee6efd3a9a","fields":{"slug":"/[BOJ]14502/"},"frontmatter":{"title":"👩‍💻 14502. 연구소"}}},{"node":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","fields":{"slug":"/[BOJ]17779/"},"frontmatter":{"title":"👩‍💻 17779. 게리맨더링 2"}}},{"node":{"id":"aa67d90b-4801-574c-b225-56c23737a5e2","fields":{"slug":"/[BOJ]2579/"},"frontmatter":{"title":"👩‍💻 2579. 계단 오르기"}}},{"node":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","fields":{"slug":"/[BOJ]14499/"},"frontmatter":{"title":"👩‍💻 14499. 주사위 굴리기"}}},{"node":{"id":"6a79cddd-dc1b-5e34-8a84-af313c37de0e","fields":{"slug":"/[BOJ]15685/"},"frontmatter":{"title":"👩‍💻 15685. 드래곤 커브"}}},{"node":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","fields":{"slug":"/[BOJ]17144/"},"frontmatter":{"title":"👩‍💻 17144. 미세먼지 안녕!"}}},{"node":{"id":"1d132410-9377-5e55-85b9-4664ff64de73","fields":{"slug":"/[BOJ]9935/"},"frontmatter":{"title":"👩‍💻 9935. 문자열 폭발"}}},{"node":{"id":"e13ff8d6-9b02-56c0-83e2-1d3144f70e1d","fields":{"slug":"/[BOJ]16472/"},"frontmatter":{"title":"👩‍💻 16472. 고냥이"}}},{"node":{"id":"0f782160-aaa9-5c54-80c0-72a15172dd16","fields":{"slug":"/[BOJ]1083/"},"frontmatter":{"title":"👩‍💻 1083. 소트"}}},{"node":{"id":"fef58615-9e18-5f50-8d09-3d1e2839342f","fields":{"slug":"/[BOJ]20922/"},"frontmatter":{"title":"👩‍💻 20922. 겹치는 건 싫어"}}},{"node":{"id":"7ca89240-c65f-5cb4-aeef-c5f1605ebd00","fields":{"slug":"/[BOJ]16172/"},"frontmatter":{"title":"👩‍💻 16172. 나는 친구가 적다(Large)"}}}]},"previous":{"fields":{"slug":"/[스프링입문]6/"},"frontmatter":{"title":"📺 6. 스프링 DB 접근 기술"}},"next":{"fields":{"slug":"/[스프링입문]7/"},"frontmatter":{"title":"📺 7. AOP"}}},"pageContext":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","series":"👩‍💻 BOJ","previousPostId":"8b5e56c2-f7dd-5d13-92c4-4e18d9df605b","nextPostId":"7012c442-42b4-5c43-8945-85bdce9799cd"}},"staticQueryHashes":[]}